//////////////////////////////////////////////////////////////////////////

#ifndef _GRAPHICSIMPL_H_
#include "graphicsimpl.h"
#endif

//////////////////////////////////////////////////////////////////////////

#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_ttf.h>

//////////////////////////////////////////////////////////////////////////


GraphicsImpl::GraphicsImpl()
{
    // Init SDL
    SDL_Init( SDL_INIT_EVERYTHING );

    // Init TFF
    TTF_Init();
    m_Font = TTF_OpenFont("rc/comic.ttf", 16);

    const int width = 640;
    const int height = 480;
    const int bitsPerPixel = 32;

    //Set up the screen
    m_Screen = SDL_SetVideoMode( width, height, bitsPerPixel, SDL_HWSURFACE|SDL_DOUBLEBUF);
}


//////////////////////////////////////////////////////////////////////////


GraphicsImpl::~GraphicsImpl()
{
    SDL_Quit();
    TTF_Quit();
}


//////////////////////////////////////////////////////////////////////////


Graphics::Object GraphicsImpl::LoadObject( const std::string& _fileName )
{
    //The image that's loaded
    SDL_Surface* loadedImage = NULL;

    //The optimized surface that will be used
    SDL_Surface* optimizedImage = NULL;

    //Load the image
    loadedImage = IMG_Load( _fileName.c_str() );

    //If the image loaded
    if( loadedImage != NULL )
    {
        //Create an optimized surface
        optimizedImage = SDL_DisplayFormatAlpha( loadedImage );

        //Free the old surface
        SDL_FreeSurface( loadedImage );
    }

    //Return the optimized surface
    return optimizedImage;
}


//////////////////////////////////////////////////////////////////////////


void GraphicsImpl::DrawObject( Graphics::Object _object, size_t _x, size_t _y, bool _center, Graphics::Object _surface )
{
    //Holds offsets
    SDL_Rect offset;

    SDL_Surface* surface = static_cast<SDL_Surface*>(_object);

    //Get offsets
    offset.x = _x;
    offset.y = _y;

    // Center object
    if ( _center )
    {
        offset.x -= (surface->w)/2;
        offset.y -= (surface->h)/2;
    }

    //Blit
    SDL_BlitSurface( surface,
                     NULL,
                     static_cast<SDL_Surface*>(_surface? _surface : m_Screen),
                     &offset );
}


//////////////////////////////////////////////////////////////////////////


void GraphicsImpl::Update()
{
    // Update the screen
    SDL_Flip( static_cast<SDL_Surface*>(m_Screen) );
}


//////////////////////////////////////////////////////////////////////////


void GraphicsImpl::DrawString( const char* _str )
{
    // Render some text in solid red to a new surface
    // then blit to the upper left of the screen
    // then free the text surface
    //SDL_Surface *screen;
    SDL_Color color={255,0,0};
    SDL_Surface *text_surface;
    if(!(text_surface=TTF_RenderText_Solid(static_cast<TTF_Font*>(m_Font),_str,color))) {
        //handle error here, perhaps print TTF_GetError at least
    } else {
        SDL_BlitSurface(text_surface,NULL,static_cast<SDL_Surface*>(m_Screen),NULL);
        //perhaps we can reuse it, but I assume not for simplicity.
        SDL_FreeSurface(text_surface);
    }
}


//////////////////////////////////////////////////////////////////////////


void GraphicsImpl::ClearScreen()
{
    // Fill the screen white
    SDL_FillRect( static_cast<SDL_Surface*>(m_Screen),
        &static_cast<SDL_Surface*>(m_Screen)->clip_rect,
        SDL_MapRGB( static_cast<SDL_Surface*>(m_Screen)->format, 0xFF, 0xFF, 0xFF ) );
}


//////////////////////////////////////////////////////////////////////////
